//
//  NetworkTool.swift
//  BQWB
//
//  Created by laibingqiang on 16/10/25.
//  Copyright © 2016年 laibingqiang. All rights reserved.
//

import UIKit
import AFNetworking
class NetworkTool: AFHTTPSessionManager {
    
    enum BQNetworkTollMethod:String {
        case get = "get"
        case post = "post"
    }
    
    static let shared:NetworkTool = {
        
        let tool = NetworkTool()
        
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
        
    }()
    //封装afn
    func request(method:BQNetworkTollMethod,URLString:String,parameters:Any?,success: @escaping (Any?) -> (),failure:@escaping (Error)->()){
        
        if method == .get {
            
            get(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                
                success(response)
                }, failure: { (_, error) in
                    
                failure(error)
            })
            
        }
        else
        {
            post(URLString, parameters: parameters, progress: nil, success: { (_, response) in
                
                success(response)
                }, failure: { (_, error) in
                    failure(error)
            })
        }
    }
    
    
    
    
    
    
    

}
//首页数据请求
extension NetworkTool{
    
    func getHomeData(sinceID:Int64,maxID:Int64,success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params:[String:Any] = ["access_token": BQOAuthViewModel.shared.access_token!,
                      "since_id":sinceID,
                      "max_id":maxID
                      ]
        
        request(method: NetworkTool.BQNetworkTollMethod.get, URLString: urlString, parameters: params, success: success, failure: failure)
    }
    
}

//OAuth相关
extension NetworkTool{
    
    func getUserAccount(code:String,success: @escaping (Any?) -> (),failure:@escaping (Error)->()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": BQAPPKEY,
            "client_secret": BQAPPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": BQREDIRECTURI
        ]
        
        request(method: NetworkTool.BQNetworkTollMethod.post, URLString: urlString, parameters: params, success: success, failure: failure)

    }
    
    func getUserInfo(model:BQOAuthModel,success: @escaping (Any?) -> (),failure:@escaping (Error)->()){
        
        // 请求 urlString
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        // 请求参数
        let params = [
            "access_token":model.access_token!,
            "uid":model.uid!
        ]
        request(method: NetworkTool.BQNetworkTollMethod.get, URLString: urlString, parameters: params, success: success, failure: failure)
    }
}
